//
//  XMTabBarController.m
//  BigJoy
//
//  Created by new-1020 on 2017/6/12.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "XMTabBarController.h"
#import "RecommendViewController.h"
#import "HomeViewController.h"
//#import "DIYViewController.h"
#import "NewDIYController.h"
#import "CommunityViewController.h"
#import "MeViewController.h"
#import "XMnavigationController.h"
#import "LoginViewController.h"
#import "NoNavViewController.h"

@interface XMTabBarController ()<UITabBarControllerDelegate>

@end

@implementation XMTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.navigationController.navigationBarHidden = YES;
    self.delegate = self;
    [self createControllers];
    [self setupAllTitleButton];
}

+ (void)load
{
    // 获取哪个类中UITabBarItem
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    // 设置按钮选中标题的颜色:富文本:描述一个文字颜色,字体,阴影,空心,图文混排
    // 创建一个描述文本属性的字典
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    [item setTitleTextAttributes:attrs forState:UIControlStateSelected];
    // 设置字体尺寸:只有设置正常状态下,才会有效果
    NSMutableDictionary *attrsNor = [NSMutableDictionary dictionary];
    attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
}

#pragma mark - 创建viewControllers
-(void)createControllers
{
      //推荐
    RecommendViewController *indexVC = [[RecommendViewController alloc] init];
    XMnavigationController *indexNav = [[XMnavigationController alloc]initWithRootViewController:indexVC];
    [self addChildViewController:indexNav];
      //主页
    HomeViewController *HomeVC = [[HomeViewController alloc] init];
    XMnavigationController *HomeNav = [[XMnavigationController alloc]initWithRootViewController:HomeVC];
    [self addChildViewController:HomeNav];
      //DIY
    NewDIYController *DIYVC = [[NewDIYController alloc] init];
    XMnavigationController *DIYNav = [[XMnavigationController alloc]initWithRootViewController:DIYVC];
    [self addChildViewController:DIYNav];
      //社区
    CommunityViewController *CommunVC = [[CommunityViewController alloc] init];
    XMnavigationController *CommunNav = [[XMnavigationController alloc]initWithRootViewController:CommunVC];
    [self addChildViewController:CommunNav];
      //我的
    MeViewController *MeVC = [[MeViewController alloc] init];
    XMnavigationController *MeNav = [[XMnavigationController alloc]initWithRootViewController:MeVC];
    [self addChildViewController:MeNav];
}

#pragma mark - 创建tabBar
- (void)setupAllTitleButton
{
    // 1:首页
    UINavigationController *nav = self.childViewControllers[0];
    nav.tabBarItem.title = @"精选";
    nav.tabBarItem.image = [UIImage imageNamed:@"jingxuan_weixuan"];
    nav.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"jingxuan_xuanzhong"];
    // 2:首页
    UINavigationController *nav1 = self.childViewControllers[1];
    nav1.tabBarItem.title = @"首页";
    nav1.tabBarItem.image = [UIImage imageNamed:@"shouye_weixuan"];
    nav1.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"shouye_xuanzhong"];
    // 3.DIY
    UINavigationController *nav3 = self.childViewControllers[2];
    nav3.tabBarItem.title = @"定制";
    nav3.tabBarItem.image = [UIImage imageNamed:@"skin_weixuan"];
    nav3.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"dingzhi_xuanzhong"];
    // 4.社区
    UINavigationController *nav4 = self.childViewControllers[3];
    nav4.tabBarItem.title = @"社区";
    nav4.tabBarItem.image = [UIImage imageNamed:@"shequ_weixuan"];
    nav4.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"shequ_xuanzhong"];
    // 5.我的
    UINavigationController *nav5 = self.childViewControllers[4];
    nav5.tabBarItem.title = @"我";
    nav5.tabBarItem.image = [UIImage imageNamed:@"wo_weixuan"];
    nav5.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"wo_xuanzhong"];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if ([UserTool isLogin]) {
        return YES;
    }
    if([((UINavigationController *)viewController).childViewControllers[0] isKindOfClass:[MeViewController class]]){
        LoginViewController * vc = [[LoginViewController alloc]init];
        XMnavigationController * nav = [[XMnavigationController alloc]initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:nil];
        return NO;
    }else {
        return YES;
    }
}

@end
